package com.lock.subscribe.msg;


import java.util.HashMap;
import java.util.Map;

import com.lock.subscribe.constant.ReportDataAllKey;
import com.lock.subscribe.util.Base64Util;
import com.lock.subscribe.util.ByteTools;
import com.lock.subscribe.util.DateUtil;
import com.lock.subscribe.util.HexUtils;
import com.lock.subscribe.util.MapToObjUtil;

public class MessageMapAllParse {
	 private static Integer typeLength = 2;
	 private static Integer randomLength = 4;
	 private static Integer serverTypeLength =4;
	 private static Integer timeLength = 8;
	 private static Integer countLength = 4;
	 private static Integer endLength = 4;
	 
	 /**
	  *
	  * @param message
	  * @return
	  */
	 public static  String getServerType(String message){
		 String serverType = message.substring(add(typeLength,randomLength),add(typeLength,randomLength,serverTypeLength));
		 return serverType;
	 }
	 /**
	  * @param message
	  * @return
	  */
	 public static  String getCmdType(String message){
		 String cmdType = message.substring(0,typeLength);
		 return ReportDataAllKey.reportDataKeyMap.get(cmdType);
	 }
	 /**
	  * @param message
	  * @return
	  */
	 public static  String getMessageCmdType(String message){
		 String data = getData(message);
		 String cmdType = data.substring(0,typeLength);
		 return ReportDataAllKey.reportDataKeyMap.get(cmdType);
	 }
	 /**
	  * @param message
	  * @return
	  */
	 public static  String getTime(String message){
		 String time = message.substring(add(typeLength,randomLength,serverTypeLength),add(typeLength,randomLength,serverTypeLength,timeLength));
		 Long date = Long.parseLong(time,16);
		 return DateUtil.stampToDate(date);
	 }	
	 public static  String getStampTime(String message){
		 String time = message.substring(add(typeLength,randomLength,serverTypeLength),add(typeLength,randomLength,serverTypeLength,timeLength));
		 return time;
	 }	
	 /**
	  * @param message
	  * @return
	  */
	 public static  String getRandom(String message){
		 String random = message.substring(typeLength,add(typeLength,randomLength));
		 return random;
	 }	
	 /**
	  * @param message
	  * @return
	  */
	 public static  String getData(String message){
		 String data = message.substring(add(typeLength,randomLength,serverTypeLength,timeLength,countLength),sub(message.length(),endLength));
		 return data;
	 }
    /**
     * @param messageLength
     * @param message
     * @return
     */
    public static Map<String,Object> parseMessage(Integer messageLength, String message){
    	if(messageLength==0){ return null ;}
        try{
            Map<String,Object> map = new HashMap<>();
            Integer index =0 ;
            while (index<messageLength){
                String flag = message.substring(index,index+=2).toLowerCase();
                String length = message.substring(index,index+=2);
                String data = message.substring(index,index+=Integer.parseInt(length,16)*2);
                if(flag.equals("19")){
                    map.put(ReportDataAllKey.reportDataKeyMap.get(flag),ByteTools.hexStringToString(data));
                }
                else{
                    map.put(ReportDataAllKey.reportDataKeyMap.get(flag),Long.valueOf(data,16));
                }
            }
           
            return map;
        }catch (Exception e){
            throw new RuntimeException(e);
        }
    }
    public static Map<String,Object> parseImageMessage(Integer messageLength, String message){
        try{
            if(messageLength==0){
                return null ;
            }
            Map<String,Object> map = new HashMap<>();
            byte[] array = Base64Util.decode(message) ;
            message = HexUtils.encode(array);
            Integer index =0 ;
            while (index<messageLength*2){
                String flag = message.substring(index,index+=2).toLowerCase();
                String length = message.substring(index,index+=4);
                String data = message.substring(index,index+=Integer.parseInt(length,16)*2);
                if(Long.valueOf(flag,16)==5L || Long.valueOf(flag,16)==19L){
                    map.put(ReportDataAllKey.reportDataKeyMap.get(flag),data);
                }else{
                    map.put(ReportDataAllKey.reportDataKeyMap.get(flag),Long.valueOf(data,16));
                }
            }
            return map;
        }catch (Exception e){
            e.printStackTrace();
            return null;
        }
    }
    /**
     * @param messageLength
     * @param message
     * @param clazz
     * @param <T>
     * @return
     */
    public static <T> T parseMessage(Integer messageLength, String message,Class clazz){
        return MapToObjUtil.returnObj(parseMessage(messageLength,message),clazz);
    }
    
    public static Integer add(Integer count ,Integer... numbers) {
    	for(Integer number:numbers) {
    		count += number;
    	}
    	return count;
    }
    public static Integer sub(Integer count ,Integer... numbers) {
    	for(Integer number:numbers) {
    		count -= number;
    	}
    	return count;
    }
    
	/**
	 ** 更换IP跟端口
	 * 
	 * @param status
	 * @param stamp
	 * @return
	 */
	public static String getChangeIpPort(String ip, String port) {
		StringBuilder sendData = new StringBuilder();
		String ipsplit[] = ip.replace(".", ",").split(",");
		for(String ipString:ipsplit) {
			sendData.append(HexUtils.toHex(Integer.valueOf(ipString), 2));
		}
		sendData.append(HexUtils.toHex(Integer.valueOf(port), 4));
		return sendData.toString();

	}
	public static void main(String[] args) {
		System.out.println("00000001".length());
	}
}
